/* 
	--------------------------------------------------------------
	lightbox_plugin.js 2012-05-16 tb@gambio
	Gambio GmbH
	http://www.gambio.de
	Copyright (c) 2012 Gambio GmbH
	Released under the GNU General Public License (Version 2)
	[http://www.gnu.org/licenses/gpl-2.0.html]
	--------------------------------------------------------------
*/
(function($){
    $.fn.lightbox_plugin = function(options){     
		
	var settings = {
		template: "",
		template_target: ".lightbox_package_content",
		className: "",
		url_param: "",
		lightbox_width: 800,
		lightbox_height: ""
	};
	
	if(options)
	{
		$.extend(settings,options);
	}
	       
	var window_width = $(window).width();
	var window_height = $(window).height();
	var lightbox_width;
	var lightbox_height;
	
	var lightbox_package = $("<div></div>").addClass("lightbox_package");
	var lightbox_package_shadow = $("<div></div>").addClass("lightbox_package_shadow");
	lightbox_package.append(lightbox_package_shadow);		
	var lightbox_package_loading_image = $("<img></img>").attr("src", "../images/loading.gif");
	lightbox_package_loading_image.css("position", "absolute").css("top", window_height/2-8).css("left", window_width/2-8);
	lightbox_package.append(lightbox_package_loading_image);
	$("body").append(lightbox_package);
	
	var lightbox_package_container = $("<div></div>").addClass("lightbox_package_container");
	var lightbox_package_border_1 = $("<b></b>").addClass("lightbox_border_1");
	var lightbox_package_border_2 = $("<b></b>").addClass("lightbox_border_2");
	var lightbox_package_border_3 = $("<b></b>").addClass("lightbox_border_3");
	var lightbox_package_border_4 = $("<b></b>").addClass("lightbox_border_4");
	var lightbox_package_border_top = $("<div></div>").addClass("lightbox_package_border").append(lightbox_package_border_1).append(lightbox_package_border_2).append(lightbox_package_border_3).append(lightbox_package_border_4);
	var lightbox_package_header = $("<div></div>").addClass("lightbox_package_header pageHeading");
	var lightbox_package_close = $("<div></div>").addClass("lightbox_package_close");
	var lightbox_package_content = $("<div></div>").addClass("lightbox_package_content");
	var lightbox_package_wrapper = $("<div></div>").addClass("lightbox_package_wrapper").append(lightbox_package_header).append(lightbox_package_close).append(lightbox_package_content);
	var lightbox_package_border_bottom = $("<div></div>").addClass("lightbox_package_border").append(lightbox_package_border_4.clone()).append(lightbox_package_border_3.clone()).append(lightbox_package_border_2.clone()).append(lightbox_package_border_1.clone());
	lightbox_package_container.append(lightbox_package_border_top).append(lightbox_package_wrapper).append(lightbox_package_border_bottom);

	fillTemplate();
      
	function fillTemplate(){
		$.ajax({
			type: "POST",
			url: "request_port.php?module=LightboxPluginAdmin",
			data: {"action": "get_template", "template": settings.template, "className": settings.className, "param": settings.url_param},
			success: function(template){
				if(template.match(/WARNING\(512\)/) || template == ""){
					lightbox_package.remove();
					alert("Smarty Error - Unable to find Template");
				}else if(template.match(/t_action_request not found/)){
					lightbox_package.remove();
					alert("Warning - Unable to call function");
				}else{
					$(".lightbox_package").append(lightbox_package_container);
					$.template( "lightboxTemplate", template );
					$.tmpl( template, settings.url_param ).appendTo( settings.template_target );
					initialize();
				}			
			},
			error: function(){
				lightbox_package.remove();
				alert("Connection Error - Unable to connect to server");
			}
		});
	}
      
	function initialize(){  
		
		lightbox_package_loading_image.hide();
		lightbox_package_shadow.show();
		lightbox_package_container.show();
		
		update_window();
		
		$(".lightbox_package_close").bind("click", function(){
			lightbox_close();
		});
		
		$(window).bind("scroll", function(){
			update_window();
		});

		$(window).bind('resize', function() {
			update_window();
		});
	}
      
	function update_window(){
        $(lightbox_package).css('position', 'absolute');
        $(lightbox_package).css('top', $(window).scrollTop());
		
		if($.browser.msie){
			$('body').css('overflow', 'hidden');
		}
		
		window_width = $(window).width();
		window_height = $(window).height();

		if(settings.lightbox_width != "auto" && settings.lightbox_width != ""){
			lightbox_width = settings.lightbox_width;
		}else if(settings.lightbox_width == "auto"){
			lightbox_width = window_width-60;
		}else{
			lightbox_width = lightbox_package_content.width()+40;
			alert(lightbox_package_content.width());
		}

		if(lightbox_width > window_width-60){
			lightbox_width = window_width-60;
		}

		lightbox_package_container.css("left", (window_width-lightbox_width)/2);

		if(settings.lightbox_height != "auto" && settings.lightbox_height != ""){
			lightbox_height = settings.lightbox_height;
		}else if(settings.lightbox_height == "auto"){
			lightbox_height = window_height-60;
		}

		if(lightbox_package_wrapper.height() > window_height-60 || lightbox_package_wrapper.height() < lightbox_package_content.height()+30){
			lightbox_package_wrapper.height(window_height-60);
			lightbox_height = window_height-60;
		}

		lightbox_package.width(window_width);
		lightbox_package.height(window_height);
		lightbox_package_shadow.width(window_width);
		lightbox_package_shadow.height(window_height);
		lightbox_package_container.width(lightbox_width);
		lightbox_package_container.height(lightbox_height);
		lightbox_package_wrapper.height(lightbox_height);
	}
	
	return false;     
	
	};  
})(jQuery);

function lightbox_close(){
    $(window).unbind("scroll");
    $(window).unbind('resize');
	if($.browser.msie){
		$('body').css('overflow', 'auto');
	}
	$(".lightbox_package").remove();
}